#include <stdio.h>
#include <limits.h>

static const int V = 100;
int G[V][V];

void init(int n)
{
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j)
			if (i == j)
				G[i][j] = 0;
			else
				G[i][j] = INT_MAX;
}

int Prim(int n)
{
	int* dist = G[0];

	int t = n;
	int amount = 0;
	while (--t)
	{
		int min = INT_MAX;
		int add;
		for (int i = 1; i < n; ++i)
		{
			if (dist[i] != 0 && dist[i] < min)
			{
				add = i;
				min = dist[i];
			}
		}

		amount += min;
		dist[add] = 0;
		for (int i = 1; i < n; ++i)
			if (G[add][i] < dist[i])
				dist[i] = G[add][i];
	}
	return amount;
}

int main()
{
	while (1)
	{
		int n;
		scanf ("%d", &n);
		if (n == 0)
			break;

		for (int i = 0; i < n * (n - 1) / 2; ++i)
		{
			int a, b, w;
			scanf ("%d %d %d", &a, &b, &w);
			--a, --b;
			G[a][b] = G[b][a] = w;
		}
		printf ("%d\n", Prim(n));
	}
	return 0;
}